package com.gen.utils.ngen;

import com.gen.utils.Pinyin4jHelper;
import com.gen.utils.lan.Hangul;
import com.gen.utils.lan.jp.RomajiHenkan;
import org.apache.commons.lang.RandomStringUtils;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class InsUsernameGen {
    public static String getSpit(){
        String sp = "";
        int index = new Random().nextInt(100);
        if(index<=15){
            sp = ".";
        }else if(index<=30){
            sp = "_";
        }else if(index<=40){
            sp = "__";
        }
        return sp;
    }
    public static String getSuffix(){
        String sp = "";
        int index = new Random().nextInt(100);
        if(index<=40){
            sp = getSpit()+ RandomStringUtils.random(1+new Random().nextInt(2),true,false).toLowerCase();
        }else if(index<=60){
            sp = getSpit()+ RandomStringUtils.random(1+new Random().nextInt(2),false,true);
        }else if(index<=80){
            sp = "_";
        }else if(index<=90){
            sp = "__";
        }
        return sp;
    }

    public static String getPreffix(){
        String sp = "";
        int index = new Random().nextInt(100);
        if(index<=50){
            sp = RandomStringUtils.random(1+new Random().nextInt(2),true,false).toLowerCase()+getSpit();
        }else if(index<=100){
            sp = RandomStringUtils.random(2,false,true)+getSpit();
        }
        return sp;
    }
    public static String spitWord(String name){

        Pattern p = Pattern.compile("[\uac00-\ud7ff\u4e00-\u9fa5]");
        Matcher matcher = p.matcher(name);
        while (matcher.find()){
            String wz = matcher.group();
            name = name.replace(wz," "+toRoma(wz));
            p.matcher(name);
        }
        return name;

    }

    public static String toRoma(String word){
        if(word.matches(".*[\uac00-\ud7ff].*")){
            return  Hangul.romanize(word);
        }
        if(word.matches(".*[\u4e00-\u9fa5].*")){
            return  Pinyin4jHelper.converterToSpell(word).split(",")[0];
        }
        return "";
    }


    public static boolean hasJapanese(String  string) {
        for(int i = 0 ;i<string.length();i++){
            Character.UnicodeBlock b = Character.UnicodeBlock.of(string.charAt(i));
            if((b == Character.UnicodeBlock.HIRAGANA ||
                    b == Character.UnicodeBlock.KATAKANA ||
                    b == Character.UnicodeBlock.KATAKANA_PHONETIC_EXTENSIONS)){
                return true;
            };
        }
        return  false;
    }

    public static String genUsername(String name,Integer sex,String cc,Integer tryTimes){
        if(tryTimes== null || tryTimes==0){
            return genUsernameByNickname(name,sex,cc);
        }else {
            int rate = new Random().nextInt(100);
            rate += tryTimes*3;
            if(rate<30){
                String username =  genUsernameByNickname(name,sex,cc);
                if(!username.matches(".+\\d{2,}$")){
                    username += getSuffix();
                }
                return username;
            }else if(rate<40){
                return getPreffix()+genUsernameByNickname(name,sex,cc);
            } else if(rate<60){
                return EngNameGenUtils.genFirstName(sex).toLowerCase()+getSuffix().toLowerCase();
            }else if(rate<85) {
                return genUsernameByGenWords();
            }else {
                return genUsernameByGenWords()+getSuffix();
            }
        }
    }

    public static String genUsernameByGenWords(){
        String username = WordGen.genWord();
        if(username.length()<6 && username.length()>=4){
            int len = 2+new Random().nextInt(3);
            username +=getSpit();
            for (int i = 0;i<len;i++) {
                username +=  new Random().nextInt(10);
            }
        }
        return username;

    }
    public static String genUsernameByNickname(String name,int sex,String cc){
        String username = name;
        if("jp".equals(cc)){
            RomajiHenkan romajiHenkan = new RomajiHenkan();
            username = romajiHenkan.convert(name,false);
        }else {
            Pattern p = Pattern.compile("[\u0600-\u06ff\u0750-\u077f\ufb50-\ufc3f\ufe70-\ufefc]");
            Matcher matcher = p.matcher(name);
            if(matcher.find()){
                username = ARABNameGenUtils.roma(name);
            }else {
                username = spitWord(name);

            }
        }
        username= username.toLowerCase();
        username =  username.replaceAll("[^a-zA-Z0-9\\._\\s]","");
        username = username.trim();
        String[] usernameArray = username.split("\\s{1,}");
        if(usernameArray.length>3){
            username = usernameArray[0]+" "+usernameArray[1]+" "+usernameArray[usernameArray.length-1];
        }
        username = username.replaceAll("\\s{1,}",getSpit());
        if(username.length()<6 && username.length()>=4){
            int len = 2+new Random().nextInt(3);
            username +=getSpit();
            for (int i = 0;i<len;i++) {
                username +=  new Random().nextInt(10);
            }
        }else if(username.length()<4){
            username = EngNameGenUtils.genFirstName(sex).toLowerCase()+getSuffix();
        }else if(username.length()>20) {
            username = username.substring(0,20);
        }
        if(username.matches("\\d*")){
            username =  RandomStringUtils.random(2,true,false).toLowerCase()+getSpit()+username;
        }
        return username;
    }
    public static void main(String[] arg){
        RomajiHenkan henkan = new RomajiHenkan();
        System.out.println(henkan.convert("lieします",false));
//        System.out.println( Pinyin4jHelper.converterToSpell("景"));
//        System.out.println("11111".replaceAll(""," "));
//            System.out.println("imjiyeon".matches(".+\\d{2,}$"));

        System.out.println(genUsername("ブルマ美少女コレクション",0,"jp",0));
        for (int i = 0;i<6000;i++){
//            System.out.println(genUsername("임지연",0,"",i%5));
//            System.out.println("");
//            System.out.println(henkan.convert("こだわりナビ/舞台魔法先生ネギま!/演劇ハイキュー/now→クリスマス・キャロル\n",false));

//            String nickname = NameGenUtils.getKoreaNameCustom(0);
//            String username  = genUsername(nickname,0,"",2);
//////            System.out.println(username);
//            System.out.println(nickname+"\t"+username+"");;
//////                        System.out.println(genUsernameByGenWords());;

        }
    }
}
